package com.qiguliuxing.dts.db.service;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.qiguliuxing.dts.db.dao.DtsPublishRecordMapper;
import com.qiguliuxing.dts.db.domain.DtsPublishRecord;

@Service
public class DtsPublishRecordService {
    @Autowired
    private DtsPublishRecordMapper dtsPublishRecordMapper;

    private String generatePublishSn() {
        // 生成格式: PUB + 时间戳 + 4位随机数
        return "PUB" + System.currentTimeMillis() + (int) ((Math.random() * 9 + 1) * 1000);
    }

    // ------------------------------------------------------wx端------------------------------------------------------
    public int createPublishRecord(DtsPublishRecord record) {
        record.setAddTime(LocalDateTime.now());
        record.setStatus(0); // 0-待接单，1-已接单，2-已完成，3-已取消
        record.setUpdateTime(LocalDateTime.now());
        record.setDeleted(false); // 默认未删除
        record.setPublishSn(generatePublishSn()); // 生成发布单编号

        return dtsPublishRecordMapper.insertSelective(record);
    }

    public List<DtsPublishRecord> getUserPublishRecords(Integer userId, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return dtsPublishRecordMapper.selectByUserId(userId);
    }

    public DtsPublishRecord getPublishDetail(Integer id) {
        return dtsPublishRecordMapper.selectByPrimaryKey(id);
    }

    public int cancelPublish(Integer userId, Integer id) {
        DtsPublishRecord record = dtsPublishRecordMapper.selectByPrimaryKey(id);
        if (record != null && record.getUserId().equals(userId)) {
            record.setStatus(3); // 3表示已取消
            return dtsPublishRecordMapper.updateByPrimaryKeySelective(record);
        }
        return 0;
    }

    public int updatePublishStatus(Integer id, Integer status) {
        DtsPublishRecord record = new DtsPublishRecord();
        record.setId(id);
        record.setStatus(status);
        return dtsPublishRecordMapper.updateByPrimaryKeySelective(record);
    }

    // ------------------------------------------------------admin端------------------------------------------------------
    public List<DtsPublishRecord> querySelective(Integer userId, String publishSn,
            Integer page, Integer limit, String sort, String order) {

        if (page != null && limit != null) {
            PageHelper.startPage(page, limit);
        }

        return dtsPublishRecordMapper.selectByCondition(
                userId,
                publishSn,
                sort,
                order);
    }
    // public List<DtsPublishRecord> querySelective(Integer userId, String
    // publishSn,
    // Integer page, Integer limit, String sort, String order) {
    // DtsPublishRecordExample example = new DtsPublishRecordExample();
    // DtsPublishRecordExample.Criteria criteria = example.createCriteria();

    // if (userId != null) {
    // criteria.andUserIdEqualTo(userId);
    // }
    // if (!StringUtils.isEmpty(publishSn)) {
    // criteria.andPublishSnLike("%" + publishSn + "%");
    // }

    // // 添加状态条件
    // criteria.andDeletedEqualTo(false);

    // // 排序
    // if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
    // example.setOrderByClause(sort + " " + order);
    // }

    // // 分页
    // if (page != null && limit != null) {
    // PageHelper.startPage(page, limit);
    // }

    // return dtsPublishRecordMapper.selectByExample(example);
    // }

    public DtsPublishRecord queryById(Integer id) {
        return dtsPublishRecordMapper.selectByPrimaryKey(id);
    }

    public int delete(Integer id) {
        if (id == null) {
            return 0;
        }

        DtsPublishRecord record = dtsPublishRecordMapper.selectByPrimaryKey(id);
        if (record == null) {
            return 0;
        }

        if (dtsPublishRecordMapper.deleteByPrimaryKey(id) == 0) {
            return 0;
        }
        return 1;
    }

    public int updateById(DtsPublishRecord record) {
        if (record == null || record.getId() == null) {
            return 0;
        }

        // 检查记录是否存在
        DtsPublishRecord existingRecord = dtsPublishRecordMapper.selectByPrimaryKey(record.getId());
        if (existingRecord == null) {
            return 0;
        }

        // 设置更新时间
        record.setUpdateTime(LocalDateTime.now());

        // 执行更新
        return dtsPublishRecordMapper.updateByPrimaryKeySelective(record);
    }

}